import { MaterialService } from 'src/app/@core/services/material/material.service';
import { Component, Input } from '@angular/core';
import { TableWidthConfig } from 'ng-devui';
import { DeviceService } from 'src/app/@core/services/device.ts/device.service';
@Component({
  selector: 'app-select-material',
  templateUrl: './select-material.component.html',
  styleUrls: ['./select-material.component.scss']
})
export class SelectMaterialComponent {
  @Input() data: any;
  isSelected = false;
  displayTableData: any[] = [];
  displayTreeData: any[];
  treeData:any;
  filteredTreeNodes:any
  option:any;//
  tableheight = '230px';
  selectData:any;
  searchName:''
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field:'radio',
        header:'选择',
        fieldType:'radio',
        fixedLeft:'0px'
      },
      {
        field: 'code',
        header: '编码',
        fieldType: 'text',
        fixedLeft:'50px'
      },
      {
        field: 'name',
        header: '名称',
        fieldType: 'text',
        fixedLeft:'150px'
      },
      {
        field: 'name',
        header: '规格',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '单位',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '成本价',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '销售价',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '描述',
        fieldType: 'text',
      },
    ]
  };
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'radio',
      width: '50px',
    },
    {
      field: 'code',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
  ]
  searchMaterialCategoryValue: any;
  partClassId: any =null;
  constructor(private DeviceService:DeviceService,private MaterialService:MaterialService) { }
  ngOnInit(): void {
    this.MaterialService.getPartClassesWithChildren(1,999).subscribe((res:any)=>{
      this.treeData = res.items
      this.displayTreeData = [{ partClassName: '全部',partClassId:null}, ...this.treeData];
    })
    this.MaterialService.getParts(this.partClassId,null,null,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }
  radio(event: any,rowItem:any){
    if(event.target.checked){
      this.isSelected = true;
      this.selectData = rowItem
    }
  }
  pageIndexChange($event: any) {
    this.pager.pageIndex=$event;
    this.MaterialService.getParts(this.partClassId,null,null,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }//切换页码
  pageSizeChange($event: any){
    this.MaterialService.getParts(this.partClassId,null,null,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }//改变每页显示数量
  search(){
    this.MaterialService.getParts(this.partClassId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }
  search2(){
    this.pager.pageIndex = 1
    this.MaterialService.getParts(this.partClassId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }
  reset(){
    this.searchName = ''
    this.MaterialService.getParts(this.partClassId,null,this.searchName,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }

  afterTreeInit($event: any){
    if($event["null"]){
      $event["null"].data.isActive=true
    }
  }//树形菜单初始化
  nodeSelected($event:any){
    this.option=$event.data.originItem
    this.partClassId = $event.data.originItem.partClassId;
    this.MaterialService.getParts(this.partClassId,null,null,this.pager.pageIndex,this.pager.pageSize).subscribe((res:any)=>{
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }//树形菜单点击事件
  searchMaterialCategory($event:any){
    this.searchMaterialCategoryValue = $event.target.value;
    if (this.searchMaterialCategoryValue === '') {
      // 如果用户输入的值为空，显示原先的数据
      this.displayTreeData = this.displayTreeData = [{ partClassName: '全部',partClassId:null}, ...this.treeData];
    } else {
      this.filteredTreeNodes = this.filterCategories(this.treeData, this.searchMaterialCategoryValue);
      this.displayTreeData = this.filteredTreeNodes
      this.displayTreeData = this.displayTreeData.map(this.setOpenTrue);
    }
  }//搜索仓库类目
  filterCategories(departments: any[], keyword: string): any[] {
    const result: any[] = [];
    for (const department of departments) {
      if (department.partClassName.includes(keyword)) {
        result.push(department);
      }
      if (department.subPartClasses.length > 0) {
        const filteredSubCategories = this.filterCategories(department.subPartClasses, keyword);
        if (filteredSubCategories.length > 0) {
          result.push(...filteredSubCategories);
        }
      }
    }
    return result;
  }
  setOpenTrue = (node: { open: boolean; subPartClasses: any[]; }) => {
    // 为当前节点添加 open: true 属性
    node.open = true;
    // 如果当前节点有子节点，递归处理子节点
    if (Array.isArray(node.subPartClasses)) {
      node.subPartClasses = node.subPartClasses.map(this.setOpenTrue);
    }
    return node;
  }
}
